<template>
  <div class="app-container">
    
<br>
<br>
    <el-button type="primary" @click="add">添加供货商</el-button>
    <br>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    > 
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="供货商名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="供货商联系人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.contactName }}
        </template>
      </el-table-column>
      <el-table-column label="供货商联系电话" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.contactPhone }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center" width="300" >
        <template slot-scope="scope">
          <el-button type="primary"  @click="getInfo(scope.row)" size="small">详情</el-button>
          <el-button type="success"  @click="update(scope.row)" size="small">修改</el-button>
          <el-button type="danger" @click="deleteInfo(scope)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="->, total, prev, pager, next"
      :total="this.total"
      :current-page="currentPage"
      :page-size="pageSize"
      @current-change="clickCurPage"
      >
    </el-pagination>
  <el-dialog title="供货商详情" :visible.sync="infoVisible">
      <h2>供货商信息</h2>
      <el-form ref="info" :model="info" label-width="120px">
        <el-form-item label="供货商名称">
            <el-input v-model="info.name" :disabled="true" />
        </el-form-item>
        <el-form-item label="供货商联系人">
            <el-input v-model="info.contactName" :disabled="true" />
        </el-form-item>
        <el-form-item label="供货商联系电话">
            <el-input v-model="info.contactPhone" :disabled="true" />
        </el-form-item>
      </el-form>
  </el-dialog>
  <el-dialog :title="dialogTitle" :visible.sync="formVisible" :closeOnClickModal="false">
      <h2>供货商信息</h2>
      <el-form ref="form" :model="form" label-width="120px" :rules="formRules">
        <el-form-item label="供货商名称" prop="name">
            <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="供货商联系人" prop="contactName">
            <el-input v-model="form.contactName" />
        </el-form-item>
        <el-form-item label="供货商联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="confirm">确 定</el-button>
          <div style="width:200px;display: inline-block;"> </div>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
  </el-dialog>
  </div>
</template>

<script>

import { getList, edit, add, del} from '@/api/supplier'

export default {
  name: "Supplier",
  filters: {

  },
  data() {
    const validateName = (rule, value, callback) => {
      if (!value) callback(new Error('请输入名称')) 
      else callback()
    }
    const validateContactName = (rule, value, callback) => {
      if (!value) callback(new Error('请输入联系人')) 
      else callback()
    }
    const validateContactPhone = (rule, value, callback) => {
      if (!value) callback(new Error('请输入联系电话')) 
      else callback()
    }
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      dialogTitle: '添加供货商',
      depts:[],
      formRules: {
        name: [{ required: true, trigger: 'blur', validator: validateName }],
        contactName: [{ required: true, trigger: 'blur', validator: validateContactName }],
        contactPhone: [{ required: true, trigger: 'blur', validator: validateContactPhone }],
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList({currentPage: this.currentPage, pageSize: this.pageSize}).then(res=>{
        console.log(res)
        this.list = res.data
        this.total = res.data.length
        this.listLoading = false
      }).catch(err=>{
        console.log("err")
        console.log(err)
        this.listLoading = false
      })
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      this.infoVisible = true
    },
    update(row) {
      this.reset()
      this.dialogTitle = '修改供货商'
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
    },
    deleteInfo(scope) {
      console.log(scope.row)
      this.$confirm('确定删除该供货商?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(scope.row.id).then(res=>{
          this.list.splice(scope.$index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(err=>{
          console.log(err)
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    add(row) {
      this.reset()
      this.dialogTitle = '添加供货商'
      this.formVisible = true
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if(this.form.id) {  // update
            console.log(this.form)
            edit(this.form).then(res=>{
              console.log(res)
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/store/supplier', name: "Supplier"});
            }).catch(err=>{
              console.log(err)
            })
          } else { // add
            console.log(this.form)
            add(this.form).then(res=>{
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/store/supplier', name: "Supplier"});
            }).catch(err=>{
              console.log(err)
            })
          }
        }
      })
    },
    cancel() {
      this.formVisible = false
    },
    reset(){
      this.form = {}
    }
  }
}
</script>
